package com.qqt.csr.ixport.task;

import com.qqt.csr.ixport.entity.BatchImportTask;
import com.qqt.csr.ixport.enums.TaskStatus;
import com.qqt.csr.ixport.service.BatchImportTaskService;
import com.qqt.csr.ixport.service.FileChunkProducer;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Author: yang hao
 * @Date: 2025/2/6 15:19
 * @Description: 任务管理器
 **/
// @Component
@Deprecated
public class TaskManager {

    private final ExecutorService executor = Executors.newCachedThreadPool();
    private final ConcurrentMap<Long, AtomicBoolean> cancellationFlags = new ConcurrentHashMap<>();
    private final BatchImportTaskService batchImportTaskService;
    private final FileChunkProducer fileChunkProducer;

    /**
     * 构造函数
     * @param batchImportTaskService 批量导入任务服务
     * @param fileChunkProducer 文件块生产者
     */
    public TaskManager(BatchImportTaskService batchImportTaskService, FileChunkProducer fileChunkProducer) {
        this.batchImportTaskService = batchImportTaskService;
        this.fileChunkProducer = fileChunkProducer;
    }

    /**
     * 启动处理任务
     * @param batchSize 批量大小
     */
    public void startProcessing(int batchSize) {
        // List<BatchImportTask> tasks = batchImportTaskService.fetchUnStartStatus(batchSize, TaskStatus.UN_START);
        // tasks.forEach(task -> {
        //     AtomicBoolean flag = new AtomicBoolean(false);
        //     if (cancellationFlags.putIfAbsent(task.getId(), flag) == null) {
        //         executor.submit(new TaskProcessor(
        //                 task, batchImportTaskService, fileChunkProducer,
        //                 new DynamicChunkStrategy(), flag
        //         ));
        //     }
        // });
    }

    /**
     * 取消任务
     * @param taskId 任务ID
     */
    public void cancelTask(Long taskId) {
        AtomicBoolean flag = cancellationFlags.get(taskId);
        if (flag != null) {
            flag.set(true);
            cancellationFlags.remove(taskId);
        }
    }

}
